package com.wyix.config;

import com.wyix.device.LEDLight;
import com.wyix.device.Light;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 灯光设备自动配置类
 * 演示文档中的@Configuration、条件注解、@EnableConfigurationProperties等知识点
 */
@Configuration
@ConditionalOnClass(Light.class) // 当Light接口存在时生效
@EnableConfigurationProperties(SmartHomeConfig.class) // 启用配置属性绑定
public class LightAutoConfiguration {

    private final SmartHomeConfig properties;

    // 构造函数注入配置属性
    public LightAutoConfiguration(SmartHomeConfig properties) {
        this.properties = properties;
    }

    /**
     * 默认LED灯光配置
     *
     * @ConditionalOnMissingBean: 当用户未自定义Light时生效
     * @ConditionalOnProperty: 当配置属性smart.home.light.enabled=true时生效
     */
    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnProperty(prefix = "smart.home.light", name = "enabled", havingValue = "true")
    public Light ledLight() {
        SmartHomeConfig.Light lightProps = properties.getLight();
        return new LEDLight(lightProps.getColor(), lightProps.getBrightness());
    }

    /**
     * 开发环境特殊灯光（彩色氛围灯）
     * 演示@Profile条件注解
     */
    @Configuration
    @ConditionalOnProperty(name = "spring.profiles.active", havingValue = "dev")
    public static class DevLightConfiguration {
        @Bean
        @ConditionalOnMissingBean
        public Light devEnvironmentLight(SmartHomeConfig properties) {
            return new LEDLight("rainbow", 100);
        }
    }
}